package com.tsing.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tsing.basic.entity.Permission;
import com.tsing.basic.mapper.PermissionMapper;
import com.tsing.basic.service.IPermissionService;
import com.tsing.basic.vo.req.*;
import com.tsing.basic.vo.resp.PermissionRespVo;
import com.tsing.common.exceptions.BusinessException;
import com.tsing.common.exceptions.ServerException;
import com.tsing.common.service.UserRedisService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author TheTsing
 * @since 2021-03-29
 */
@Service
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper, Permission> implements IPermissionService {

    @Autowired
    private UserRedisService userRedisService;

    @Override
    public void addPermission(PermissionAddReqVo reqVo) {
        if (0 != baseMapper.selectCount(new QueryWrapper<Permission>().eq("url", reqVo.getUrl()))) {
            throw new BusinessException(ServerException.Proxy.PERMISSIONURL_IS_NOTNULL);
        }
        Permission permission = new Permission();
        BeanUtils.copyProperties(reqVo, permission);
        permission.setDescription(reqVo.getRemark());
        baseMapper.insert(permission);
    }

    @Override
    public void deletePermission(IdsReqVo reqVo) {
        if (!StringUtils.isBlank(baseMapper.haveRelevance(reqVo.getIds()))) {
            throw new BusinessException(ServerException.Proxy.PERMISSION_HAVE_RELEVANCE);
        }
        baseMapper.deleteBatchIds(reqVo.getIds());
    }

    @Override
    public PermissionRespVo getPermissionById(IdReqVo reqVo) {
        Permission permission = baseMapper.selectById(reqVo.getId());
        PermissionRespVo permissionRespVo = new PermissionRespVo();
        BeanUtils.copyProperties(permission, permissionRespVo);
        permissionRespVo.setRemark(permission.getDescription());
        return permissionRespVo;
    }

    @Override
    public void updatePermission(PermissionUpdateReqVo reqVo) {
        if (0 != baseMapper.selectCount(new QueryWrapper<Permission>().eq("url", reqVo.getUrl())
                .ne("id", reqVo.getId()))) {
            throw new BusinessException(ServerException.Proxy.PERMISSIONURL_IS_NOTNULL);
        }
        Permission permission = new Permission();
        BeanUtils.copyProperties(reqVo, permission);
        permission.setDescription(reqVo.getRemark());
        baseMapper.updateById(permission);
        userRedisService.savePermission2Redis(baseMapper.selectAllpermission());
    }

    @Override
    public Page<PermissionRespVo> permissionPage(PermissionPageReqVo reqVo) {
        Page<Permission> page = new Page<>(reqVo.getPageNum(), reqVo.getPageSize());
        List<PermissionRespVo> list = baseMapper.permissionPage(reqVo, page);
        Page<PermissionRespVo> permissionPage = new Page<>();
        permissionPage.setTotal(page.getTotal());//总条数
        permissionPage.setCurrent(page.getCurrent());//页码
        permissionPage.setSize(page.getSize());//当前页个数
        permissionPage.setRecords(list);//当前页数据
        return permissionPage;
    }

}
